//
//  TopSwitchView.swift
//  KkmhSwift
//  顶部滑块
//  Created by tenltrs on 2018/11/8.
//  Copyright © 2018 Tenltrs. All rights reserved.
//

import UIKit

protocol TopSwitchViewDelegate : NSObjectProtocol{
    
    func selectActionWithIndex(index : NSInteger)
}

class TopSwitchView: UIView {
    
    private var sliderView : UIImageView!
    
    private var titles : [String]!
    
    private var leftView : UILabel!
    
    private var rightView : UILabel!
    
    weak var delegate : TopSwitchViewDelegate?
    
    private let padding = 1
    
    private var color : UIColor?
    
    private var selectColor : UIColor?
    
    
    private var _on : Bool? = true
    var on : Bool?{
        set{
            _on = newValue
            if _on==true {
                self.selectItem(index: 1)
            }else{
                self.selectItem(index: 0)
            }
        }
        get{
            return _on!
        }
    }
    
    init(frame: CGRect, backgroundImage : UIImage, sliderImage : UIImage, titles : [String],fontColor : UIColor, selectFontColor : UIColor,defaultSelect : NSInteger) {
    
        super.init(frame: frame)
        
        weak var ws = self
        
        color=fontColor
        selectColor=selectFontColor
        
        self.snp.makeConstraints { (make) in
            make.size.equalTo(frame.size)
        }
        
        self.backgroundColor=UIColor(patternImage: backgroundImage)
        
        self.sliderView = UIImageView.init(image: sliderImage)
        self.addSubview(self.sliderView)
        self.sliderView.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            if defaultSelect == 0{
                make.left.equalToSuperview().offset(padding)
            }else{
                make.right.equalToSuperview().offset(-padding)
            }
            
        }
        
        self.leftView=UIViewTool.mas_label(text: titles.first!, textColor:defaultSelect==0 ? selectFontColor : fontColor, textAligment: .center, fontSize: 0).then({ (make) in
            make.font=UIFont.boldSystemFont(ofSize: 14)
        })
        self.addSubview(self.leftView!)
        self.leftView?.snp.makeConstraints({ (make) in
            make.centerY.equalToSuperview()
            make.centerX.equalTo(ws!.snp.left).offset(frame.width/4)
        })
        
        self.rightView=UIViewTool.mas_label(text: titles.last!, textColor:defaultSelect==1 ? selectFontColor : fontColor, textAligment: .center, fontSize: 0).then({ (make) in
            make.font=UIFont.boldSystemFont(ofSize: 14)
        })
        self.addSubview(self.rightView!)
        self.rightView?.snp.makeConstraints({ (make) in
            make.centerY.equalToSuperview()
            make.centerX.equalTo(ws!.snp.right).offset(-frame.width/4)
        })
        
        
        let tap = UITapGestureRecognizer.init(target: self, action: #selector(clickAction(tap:)))
        self.addGestureRecognizer(tap)
        
        self.layoutIfNeeded()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    @objc private func clickAction(tap : UITapGestureRecognizer){
         
        let point = tap.location(in: tap.view)
        
        if (point.x > frame.size.width/2){

            self.on=true
            self.delegate?.selectActionWithIndex(index: 1)
        }else{
            self.on=false
            self.delegate?.selectActionWithIndex(index: 0)
        }
    }
    
    @objc private func selectItem(index : NSInteger){
        weak var ws = self
        if index==0 {
            
            self.sliderView.snp.remakeConstraints { (make) in
                make.centerY.equalToSuperview()
                make.left.equalToSuperview().offset(padding)
            }
            CommUtls.gcd_perform(after: 0.1) {
                self.rightView?.textColor=ws?.color
            }
            
        }else{
            self.sliderView.snp.remakeConstraints { (make) in
                make.centerY.equalToSuperview()
                make.right.equalToSuperview().offset(-padding)
            }
            CommUtls.gcd_perform(after: 0.1) {
                self.leftView?.textColor=ws?.color
            }

        }
        
        UIView.animate(withDuration: 0.2, animations: {
            self.layoutIfNeeded()

        }) { (ok) in
            
            if index == 0{
                
                ws!.leftView?.textColor=ws?.selectColor
            }else{
                ws!.rightView?.textColor=ws?.selectColor
            }
        }
    }
    
}
